//
//  playerPayPriceView.m
//  SoundPill
//
//  Created by 宋旭东 on 16/8/24.
//  Copyright © 2016年 宋旭东. All rights reserved.
//
#define Margin 20
#import "playerPayPriceView.h"
#import "UIViewExt.h"
@implementation playerPayPriceView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}
- (void)createSubviews
{
    self.priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(Margin, 0, 5 * Margin, self.height)];
    _priceLabel.textColor = [UIColor colorWithRed:0.533 green:0.533 blue:0.533 alpha:1.00];
//    _priceLabel.text = @"300元";
    _priceLabel.textAlignment = NSTextAlignmentCenter;
    _priceLabel.font = [UIFont boldSystemFontOfSize:16];
    [self addSubview:_priceLabel];
    
    self.subtractButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _subtractButton.frame = CGRectMake(_priceLabel.right + _priceLabel.width, 2, self.height - 4, self.height - 4);
    [_subtractButton setImage:[UIImage imageNamed:@"player_pay_sub_shallow"] forState:UIControlStateNormal];
    [_subtractButton addTarget:self action:@selector(_subtractButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_subtractButton];
    
    self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(_subtractButton.right, 0, 2 * Margin, self.height)];
    _countLabel.text = @"0";
    _countLabel.textColor = [UIColor colorWithRed:0.459 green:0.459 blue:0.459 alpha:1.00];
    _countLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_countLabel];
    
    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _addButton.frame = CGRectMake(_countLabel.right, 2, _subtractButton.width, _subtractButton.height);
    [_addButton setImage:[UIImage imageNamed:@"player_pay_add_deep"] forState:UIControlStateNormal];
    [_addButton addTarget:self action:@selector(_addButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_addButton];
}
- (void)_subtractButtonClick:(UIButton *)sender
{
    if (_countLabel.text.intValue != 0) {
        [_subtractButton setImage:[UIImage imageNamed:@"player_pay_sub_deep"] forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
         _countLabel.text = [NSString stringWithFormat:@"%d",_countLabel.text.intValue - 1];
        if (_countLabel.text.intValue == 0) {
            [_subtractButton setImage:[UIImage imageNamed:@"player_pay_sub_shallow"] forState:UIControlStateNormal];
            _priceLabel.textColor = [UIColor colorWithRed:0.533 green:0.533 blue:0.533 alpha:1.00];
        } else {
            _priceLabel.textColor = [UIColor colorWithRed:1.000 green:0.000 blue:0.000 alpha:1.00];
        }
    } else {
       
    }
    _countChangeBlock();
}
- (void)_addButtonClick:(UIButton *)sender
{
//     if (_countLabel.text.intValue != 9) {
//         [_subtractButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//         [_addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
         _countLabel.text = [NSString stringWithFormat:@"%d",_countLabel.text.intValue + 1];
    if (_countLabel.text.integerValue > 0) {
        _priceLabel.textColor = [UIColor colorWithRed:1.000 green:0.000 blue:0.000 alpha:1.00];
        [_subtractButton setImage:[UIImage imageNamed:@"player_pay_sub_deep"] forState:UIControlStateNormal];
    }
//         if (_countLabel.text.intValue == 9) {
//             [_addButton setTitleColor:[UIColor colorWithWhite:0.619 alpha:1.000] forState:UIControlStateNormal];
//         }
//         if (_countLabel.text.intValue == 0) {
//             [_subtractButton setTitleColor:[UIColor colorWithWhite:0.619 alpha:1.000] forState:UIControlStateNormal];
//             _priceLabel.textColor = [UIColor colorWithRed:0.533 green:0.533 blue:0.533 alpha:1.00];
//         } else {
//             _priceLabel.textColor = [UIColor colorWithRed:1.000 green:0.000 blue:0.000 alpha:1.00];
//         }
//     } else {
//         
//     }
    _countChangeBlock();
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
